package ch7;

import java.util.Scanner;

public class Chihuo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final double NO_SEND_MONEY = 80;
		// 吃货联盟订餐系统
		String[] dishes = { "蒸熊掌", "烧花鸭", "烤全羊", "佛跳墙" };
		double[] prices = { 79, 38, 198, 99 };
		int choosenum;
		String[][] bills = new String[10][];
		String name;
		int pcs;// 份数
		int times;// 时间
		String address;// 地址
		// 订单状态
		int[] like = { 11, 22, 33, 44 };
		int choose;// 返回值，为0返回
		do {
			System.out.println("欢迎使用吃货联盟订餐系统");
			System.out.println("1.我要订餐");
			System.out.println("2.查看餐袋");
			System.out.println("3.签收订单");
			System.out.println("4.删除订单");
			System.out.println("5.我要点赞");
			System.out.println("6.退出系统");
			System.out.println("请选择：");
			choose = sc.nextInt();
			switch (choose) {
			case 1:
				System.out.println("我要订餐");
				System.out.println("请输入订餐人姓名");
				name = sc.next();
				System.out.println("序号\t菜品\t单价");
				for (int i = 0; i < dishes.length; i++) {
					System.out.println(i + 1 + "\t" + dishes[i] + "\t" + prices[i] + "元");
				}
				do {
					System.out.println("请选择订餐的编号");
					choosenum = sc.nextInt();
					if (choosenum > 0 && choosenum <= dishes.length) {
						break;
					}
					System.out.println("输入错误，请重新输入");
				} while (true);
				do {
					System.out.println("请输入要订餐的份数");
					pcs = sc.nextInt();
					if (pcs > 0) {
						break;
					}
					System.out.println("份数不能小于0，请重新输入");
				} while (true);
				do {
					System.out.println("请输入订餐时间（10-20整）：");
					times = sc.nextInt();
					if (times >= 10 && times <= 20) {
						break;
					}
					System.out.println("您输入的时间不符合营业范围，请重新输入");
				} while (true);
				System.out.println("请输入收货地址");
				address = sc.next();
				System.out.println("订餐成功");
				System.out.println("你订的是:" + dishes[choosenum - 1]);
				System.out.println("送餐时间:" + times + "点");
				int sendMoney = 0;// 付款
				double dishPrices = pcs * prices[choosenum - 1];// 餐费
				if (dishPrices < NO_SEND_MONEY) {
					sendMoney = 6;
				}
				double totalPrices = dishPrices + sendMoney;
				System.out.println("餐费：" + dishPrices + "元" + "送餐费" + sendMoney + "元" + "总计：" + totalPrices + "元");
				/// 先遍历订单，如果为空，则插入；
				for (int i = 0; i < bills.length; i++) {
					if (bills[i] == null) {
						bills[i] = new String[6];
						bills[i][0] = name;
						bills[i][1] = dishes[choosenum - 1] + " " + pcs + "份";
						bills[i][2] = times + "点";
						bills[i][3] = address;
						bills[i][4] = totalPrices + "元";
						bills[i][5] = "已预订";
						break;
					}
				}
				System.out.print("输入0返回：");
				choose = sc.nextInt();
				break;
			case 2:
				System.out.println("查看餐袋");
				System.out.println("序号\t订餐人\t餐品信息\t送餐日期\t送餐地址\t总金额\t订单状态");
				for (int i = 0; i < bills.length; i++) {
					if (bills[i] == null) {
						break;
					}
					System.out.println(i + 1 + "\t" + bills[i][0] + "\t" + bills[i][1] + "\t" + bills[i][2] + "\t"
							+ bills[i][3] + "\t" + bills[i][4] + "\t" + bills[i][5]);
				}
				System.out.print("输入0返回：");
				choose = sc.nextInt();
				break;
			case 3:
				System.out.println("签收订单");
				if (bills[0] == null) {
					System.out.println("订单为空，不可签收");
					System.out.println("请输入0返回：");
					choose = sc.nextInt();
					break;
				}
				System.out.println("序号\t订餐人\t餐品信息\t总金额\t订单状态");
				int i;
				for (i = 0; i < bills.length; i++) {
					if (bills[i] == null) {
						break;
					}
					System.out.println(
							i + 1 + "\t" + bills[i][0] + "\t" + bills[i][1] + "\t" + bills[i][4] + "\t" + bills[i][5]);
				}
				int signNum;
				do {
					System.out.println("请选择需要签收的订单编号：");
					signNum = sc.nextInt();
					if (signNum > 0 && signNum <= i)
						break;
					System.out.println("你的输入有误，重新输入！");
				} while (true);
				if (bills[signNum - 1][5].equals("已完成")) {
					System.out.println("订单已经完成签收，不需要重复签收");
				} else {
					bills[signNum - 1][5] = "已完成";
				}
				System.out.print("输入0返回：");
				choose = sc.nextInt();
				break;
			case 4:
				System.out.println("删除订单");
				// 删除订单需要考虑，选择的订单删除后，后面的订单前移
				// 未完成的订单不能删除
				if (bills[0] == null) {
					System.out.println("订单为空，不可删除");
					System.out.println("请输入0返回：");
					choose = sc.nextInt();
					break;
				}
				System.out.println("序号\t订餐人\t餐品信息\t总金额\t订单状态");
				int k;
				for (k = 0; k < bills.length; k++) {
					if (bills[k] == null)
						break;
					System.out.println(
							k + 1 + "\t" + bills[k][0] + "\t" + bills[k][1] + "\t" + bills[k][4] + "\t" + bills[k][5]);
				}
				int delNum;
				do {
					System.out.println("请选择要删除的订单编号");
					delNum = sc.nextInt();
					if (delNum > 0 && delNum <= k)
						break;
					System.out.println("输入有误，请重新输入");
				} while (true);
				if (!bills[delNum - 1][5].equals("已完成")) {
					System.out.println("未完成的订单不能删除！");
				} else {
					for (int s = delNum - 1; bills[s] != null; s++) {
						if (delNum == bills.length) {
							bills[delNum - 1] = null;
							// ?
						} else {
							bills[s] = bills[s + 1];
						}
					}
					System.out.println("删除完成");
				}
				System.out.print("输入0返回：");
				choose = sc.nextInt();
				break;
			case 5:
				System.out.println("我要点赞");
				System.out.println("序号\t菜品\t单价");
				for (int j = 0; j < dishes.length; j++) {
					System.out.println(j + 1 + "\t" + dishes[j] + "\t" + prices[j] + "元");
				}
				int likeNum;
				do {
					System.out.print("请选择要点赞的菜品：");
					likeNum = sc.nextInt();
					if (likeNum > 0 && likeNum <= like.length)
						break;
					System.out.println("没有这个菜品，请重新输入");
				} while (true);
				like[likeNum - 1]++;
				System.out.println("谢谢点赞！");
				System.out.print("输入0返回：");
				choose = sc.nextInt();
				break;
			}
		} while (choose == 0);
		System.out.println("谢谢使用，欢迎下次光临");
	}
}
